package com.android.djlmapp.activity.home.web;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.support.constraint.ConstraintLayout;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.webkit.WebChromeClient;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.android.djlmapp.R;
import com.android.djlmapp.activity.home.web.base.BaseWebActivity;
import com.android.djlmapp.activity.home.web.base.BaseWebChromeClient;
import com.android.djlmapp.activity.order.OrderListActivity;
import com.android.djlmapp.http.ComPareUtils;
import com.android.djlmapp.http.GlobalUrls;
import com.android.djlmapp.token.GetUrlStr;

import butterknife.BindView;

public class BuyPhoneActivity extends BaseWebActivity {

    @BindView(R.id.ll_top_back)
    LinearLayout llTopBack;
    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.home_iv_readpoint)
    ImageView homeIvReadpoint;
    @BindView(R.id.iv_top_msg)
    ConstraintLayout ivTopMsg;
    @BindView(R.id.iv_top_set)
    ImageView ivTopSet;
    @BindView(R.id.iv_top_kf)
    ImageView ivTopKf;
    private int requestCode = 2;

    @Override
    protected void initView() {
        setWebViewClient(mWebViewClient);
        llTopBack.setVisibility(View.VISIBLE);
        ivTopMsg.setVisibility(View.GONE);
        ivTopSet.setVisibility(View.GONE);
        ivTopKf.setVisibility(View.GONE);
        tvName.setText("购买手机");
        String signUrl = new ComPareUtils.ComPareBuilder()
                .put("token", getToken())
                .put("version", getVersion())
                .create().getSignString();

        String url = GlobalUrls.NEW_SHOP_PHONE +
                "?token=" + getToken() + "&version=" +
                getVersion() + "&sign=" + signUrl;
//        setWebChromeClient(mDefaultWebChromeClient); // 设置标题
        setWebUrl(url);

        super.initView();
    }
    private WebChromeClient mDefaultWebChromeClient = new BaseWebChromeClient() {
        @Override
        public void onReceivedTitle(WebView view, String title) {
            super.onReceivedTitle(view, title);
            tvName.setText("购买手机");
        }
    };
    private boolean isBack = false;

    private WebViewClient mWebViewClient = new WebViewClient() {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String str) {
            if (str.contains("login_out")) {
                GetUrlStr.getStrUrl(BuyPhoneActivity.this);
                return true;
            } else if (str.contains("order/index")) {
                // 跳到订单页
                startActivity(new Intent(BuyPhoneActivity.this, OrderListActivity.class));
                finish();
                return true;
            } else if (str.contains("djlm_new_api/get_paypwd")) {
                // 忘记密码
                startActivity(new Intent(BuyPhoneActivity.this,
                        ForgetPwdActivity.class));
                return true;
            } else if (str.contains("http://web2.djlm.wang/app5/address")) {
                // 新建地址
                startActivityForResult(new Intent(BuyPhoneActivity.this,
                        AddressAddActivity.class), requestCode);
                return true;
            } else if (str.contains("Terminal/index")) {
                mAgentWeb.back();
                return true;
            } else {
                return false;
            }
        }

        @SuppressLint("NewApi")
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
            String str = request.getUrl().toString();

            if (str.contains("login_out")) {
                GetUrlStr.getStrUrl(BuyPhoneActivity.this);
                return true;
            } else if (str.contains("order/index")) {
                // 跳到订单页
                startActivity(new Intent(BuyPhoneActivity.this, OrderListActivity.class));
                finish();
                return true;
            } else if (str.contains("djlm_new_api/get_paypwd")) {
                // 忘记密码
                startActivity(new Intent(BuyPhoneActivity.this,
                        ForgetPwdActivity.class));
                return true;
            } else if (str.contains("http://web2.djlm.wang/app5/address")) {
                // 新建地址
                startActivityForResult(new Intent(BuyPhoneActivity.this,
                        AddressAddActivity.class), requestCode);
                return true;
            } else if (str.contains("Terminal/index")) {
                mAgentWeb.back();
                return true;
            } else {
                return false;
            }
        }

        @Override
        public void onLoadResource(WebView view, String url) {
            super.onLoadResource(view, url);
            String str = view.getUrl();
            if (str.contains("/app5/terminal/new_index")) {
                isBack = true;
            }else{
                isBack = false;
            }
        }
    };

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_BACK) {
            if (mAgentWeb != null) {
                if (!mAgentWeb.back() || isBack) {
                    finish();
                }
            } else {
                finish();
            }
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    @Override
    public void btnBack() {
        if (mAgentWeb != null) {
            if (!mAgentWeb.back() || isBack) {
                finish();
            }
        } else {
            finish();
        }
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == RESULT_OK) {
            if (requestCode == 2) {
                // 刷新webview
                mAgentWeb.getUrlLoader().reload();
            }
        }
    }
}
